﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using RED_NET_Core.Utilty;
namespace RED_NET.ContentPage
{
    public partial class ShowLetter : System.Web.UI.Page
    {
        private static Users user;
        public static int idTo;
        public static string toName;
        public static int isReply = 0;
        public static int visitId;
        protected void Init()
        {
            Session["PageName"] = "ShowLetter";
            if (Session["USER"] == null)
            {
                Response.Redirect("~/ContentPage/Login_2.aspx");
            }
            else
                user = (Users)Session["USER"];
            int _newsId = -1;
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
          if (visitId != user.UserId)
              Response.Redirect("~/ContentPage/Error/PageError.aspx");
          
            if (Request.QueryString["_NewsId"] != null)
            {
                _newsId = Int32.Parse(Request.QueryString["_NewsId"]);
            }
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            News _news = new News();
            try
            {
                _news = News_BLL.ShowNews(_newsId);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            if (_news.Level != 3 || (News_BLL.JudgeRecvLetter(user.UserId, _news.Id) == 0 && _news.UId != user.UserId))
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }

            Reply.Visible = false;
            Label_LetterContent.Text = "<br/><br/><br/><br/>" + _news.Content + "<br/><br/><br/><br/>";
            Label_LetterTitle.Text = _news.Title;
            Users _user = new Users();
            LoginBLL _login = new LoginBLL();
            try
            {
                _user = _login.GetMessageOfUser(_news.UId);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            Label_LetterFrom.Text = "发件人:" + _user.NickName;
            Label_Time.Text = _news.AddTime.ToString();
            Label_LetterTo.Text = "收件人:" + _news.SendTo;
            if (_user.UserId == user.UserId)
            {

                LinkButton_Reply.Visible = false;
            }
            else
            {
                LinkButton_Reply.Text = "回复";
                isReply = 0;
                toName = _user.NickName + ";";
                idTo = _news.UId;
                try
                {
                    News_BLL.SetLetterIsRead(Int32.Parse(Request.QueryString["_Id"]));
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                LinkButton_Reply.Visible = true;
            }
            LinkButton_Inbox.PostBackUrl += "?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendletter.PostBackUrl += "?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendbox.PostBackUrl += "?VISIT_ID=" + visitId.ToString();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测用户是否登录

            if (Session["USER_ID"] == null)
            {
                Response.Write("<script>('请先登录！');location.href='Index.aspx';</script>");

            }
            if (!IsPostBack)
            {
                Init();
            }
        }
        protected void LinkButton_Reply_Click(object sender, EventArgs e)
        {
            if (isReply == 0)
            {
                isReply = 1;
                LinkButton_Reply.Text = "取消回复";
                Reply.Visible = true;
            }
            else
            {
                isReply = 0;
                LinkButton_Reply.Text = "回复";
                Reply.Visible = false;
            }
        }
        protected void LinkButton_Reply_Send_Click(object sender, EventArgs e)
        {
            string _title = TextBox_Reply_Title.Text;
            string _content = TextBox_Reply_Content.Text;
            ReturnValue retTitle = Safety.JudgeStr(_title, 1);
            ReturnValue retContent = Safety.JudgeStr(_content, 3);
            if (retContent.value == 0 && retTitle.value == 0)
            {

                int[] _sendTo = new int[2];
                _sendTo[0] = idTo;
                try
                {
                    News_BLL.AddLetter(user.UserId, _content, _title, _sendTo, 1, toName);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                TextBox_Reply_Content.Text = "";
                TextBox_Reply_Title.Text = "";
                Label_SendSuccess.Visible = true;
            }
            else
            {
                Label_Content_Error.Text = retContent.error;
                Label_Title_Error.Text = retTitle.error;

            }
        }
    }
}
